package com.wangxin.controllers;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.upload.UploadFile;
import com.wangxin.common.WxConfig;
import com.wangxin.model.LETTER;
import com.wangxin.model.ShortMsg;
import com.wangxin.model.USER;
import com.wangxin.services.WxScves;
import com.wangxin.utils.CreateVerifyCode;
import com.wangxin.utils.SnowflakeIdGenerator;

/**
 * 处理网信的表单和查询
 * 详见 JFinal 俱乐部: http://jfinal.com/club
 * 
 * WxController
 */
public class WxController extends Controller {
	
	private WxScves wxscves = new WxScves();
	
	private String baseUploadPath = WxConfig.baseUploadPath;
	
	public void index() {
		redirect("/wx/wxbd.html");
	}
	
	public void wxbd() {
		if(StringUtils.isBlank(this.baseUploadPath)){
			this.baseUploadPath = getRequest().getServletContext().getRealPath("/");
		}
		String uploadPath = DateFormatUtils.format(new Date(), "yyyyMMdd");
		File uploadPathDir = new File(this.baseUploadPath+File.separator + uploadPath);
		if(!uploadPathDir.exists()){
			uploadPathDir.mkdir();
		}
		setAttr("jobs", wxscves.getSysDictsByCategory("job"));
		setAttr("politicalStatues", wxscves.getSysDictsByCategory("political_status"));
		setAttr("nationalities", wxscves.getSysDictsByCategory("nationality"));
		setAttr("ethnicities", wxscves.getSysDictsByCategory("ethnicity"));
		setAttr("xfmddms", wxscves.getSysDictsByCategory("xf_target"));
		setAttr("idTypes", wxscves.getSysDictsByCategory("id_type"));
		render("/wx/wxbd.html");
	}
	
	public void cxxj() {
		String xfjbh = getPara("xfjbh");
		String cxm = getPara("cxm");
		if(StringUtils.isNotBlank(xfjbh) && StringUtils.isNotBlank(cxm)){
			LETTER letter = LETTER.dao.findFirst("SELECT * FROM XF_LETTER WHERE F_CENTRAL_ID = ? AND CXM = ?", xfjbh, cxm);
			setAttr("lett", letter);
			setAttr("xfjbh", xfjbh);
			setAttr("cxm", cxm);
		}
		render("/wx/cxxj2.html");
	}
	
	public void uploadFile(){
		String uploadPath = DateFormatUtils.format(new Date(), "yyyyMMdd");

		UploadFile file = getFile("file", uploadPath);
		String id = wxscves.saveImage(file.getFile(), file.getFileName().split("\\.")[1], uploadPath);

		renderJson("{\"result\":\"succ\",\"id\":\""+id+"\"}");
	}
	
	public void sendMsg(){
		String phone = getPara("sendphone");
		//生成验证码
		String verifycode = CreateVerifyCode.getCode();
		 setSessionAttr("verifycode", verifycode);
		//发送验证码
		ShortMsg sm = new ShortMsg();
		sm.setSismsid(SnowflakeIdGenerator.getNextId(1,1)+"");
		sm.setExtcode(PropKit.get("sms.extCode"));
		sm.setApplicationid(PropKit.get("sms.appId"));
		sm.setRequesttime(new Date());
		sm.setDestaddr(phone);
		sm.setMessagecontent(String.format(PropKit.get("sms.template"),verifycode));
		sm.sendMsg();
//		renderJson("{\"result\":\"succ\", \"verifycode\":\""+verifycode+"\"}");
		renderJson("{\"result\":\"succ\"");
	}
	
	public void verifyCode(){
		String code = getPara("verifycode");
		String sCode = getSessionAttr("verifycode");
		removeSessionAttr("verifycode");
		if(code.equals(sCode)){
			renderJson("{\"result\":\"succ\"}");
		}else{
			renderJson("{\"result\":\"fail\"}");
		}
	}
	
	public void saveWxbd(){
		final LETTER letter= getBean(LETTER.class, "letter");
		final USER user = getBean(USER.class, "user");
				
		//生成信访件编号
		wxscves.addWxNote(letter, user);
		setAttr("xfjbh", letter.getFCentralId());
		setAttr("cxm", letter.getCxm());
		render("/wx/bdResult.html");
	}
	
	public void queryWxInfo(){
		String xfjbh = getPara("xfjbh");
		String cxm = getPara("cxm");
		List<LETTER> letters = LETTER.dao.find("SELECT * FROM XF_LETTER WHERE F_CENTRAL_ID = ? AND CXM = ?", xfjbh, cxm);
		Map<String, Object> rtn = new HashMap<String, Object>();
		rtn.put("data", letters);
		rtn.put("count", letters.size());
		renderJson(rtn);
	}
}





